package bud.taskmanager.task;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import bud.taskmanager.task.dto.Code;
import bud.taskmanager.task.dto.Task;
import bud.taskmanager.task.service.TaskService;

@Controller
public class TaskController {

	@Autowired
	private TaskService taskService;

	@RequestMapping(value = "/tasks", method = RequestMethod.GET)
	public String list(ModelMap model) {
		model.addAttribute("tasks", taskService.findAllTasks());
		return "task/list";
	}

	@RequestMapping(value = "/tasks/new", method = RequestMethod.GET)
	public String form(ModelMap model) {
		Task task = new Task();
		task.setStartDate(formattedDate(new Date(), "yyyy-MM-dd"));
		model.addAttribute("task", task);
		return "task/form";
	}

	@RequestMapping(value = "/tasks/new", method = RequestMethod.POST)
	public String formSubmit(@ModelAttribute("task") @Valid Task task,
			BindingResult result) {
		if (result.hasErrors()) {
			return "task/form";
		}
		if (task.getId() > 0) {
			taskService.updateTask(task);
		} else {
			taskService.createTask(task);
		}
		return "redirect:";
	}

	@RequestMapping(value = "/tasks/edit/{id}", method = RequestMethod.GET)
	public String edit(@PathVariable("id") int id, ModelMap model) {
		model.addAttribute("task", taskService.findTask(id));
		return "task/form";
	}

	@RequestMapping(value = "/tasks/delete/{id}")
	public String delete(@PathVariable("id") int id) {
		taskService.deleteTask(id);
		return "redirect:..";
	}

	@RequestMapping(value = "/tasks/{id}", method = RequestMethod.GET)
	public String view(@PathVariable("id") int id, ModelMap model) {
		model.addAttribute("task", taskService.findTask(id));
		return "task/view";
	}

	@ModelAttribute("trackers")
	public List<Code> trackers() {
		List<Code> list = new ArrayList<Code>();
		list.add(new Code("1", "작업"));
		list.add(new Code("2", "이슈"));
		list.add(new Code("3", "버그"));
		list.add(new Code("4", "리뷰"));
		list.add(new Code("5", "연구"));
		list.add(new Code("6", "WBS"));
		return list;
	}

	@ModelAttribute("status")
	public List<Code> status() {
		List<Code> list = new ArrayList<Code>();
		list.add(new Code("1", "신규"));
		list.add(new Code("2", "진행"));
		list.add(new Code("3", "해결"));
		list.add(new Code("4", "의견"));
		list.add(new Code("5", "지연"));
		return list;
	}

	@ModelAttribute("priorities")
	public List<Code> priorities() {
		List<Code> list = new ArrayList<Code>();
		list.add(new Code("1", "보통"));
		list.add(new Code("2", "낮음"));
		list.add(new Code("3", "높음"));
		list.add(new Code("4", "긴급"));
		list.add(new Code("5", "즉시"));
		return list;
	}

	private String formattedDate(Date date, String format) {
		SimpleDateFormat formatter = new SimpleDateFormat(format, Locale.KOREA);
		return formatter.format(date);
	}
}
